//
//  TradeOrderUserCenterHeaderView.swift
//  Exchange
//
//  Created by rui on 2018/5/22.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

protocol OrderUserCenterHeaderDelegate: class {
    func trustClick(header: TradeOrderUserCenterHeaderView)
    func shieldClick(header: TradeOrderUserCenterHeaderView)
}

class TradeOrderUserCenterHeaderView: UIView {

    @IBOutlet weak var icon: UIImageView!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var exchange: UILabel!
    @IBOutlet weak var good: UILabel!
    @IBOutlet weak var trustNum: UILabel!
    @IBOutlet weak var shield: UIButton!
    @IBOutlet weak var trust: UIButton!
    
    var isTrust = false {
        didSet {
            self.changeTrust()
        }
    }
    
    var isShield = false {
        didSet {
            self.changeShield()
        }
    }
        
    weak var delegate: OrderUserCenterHeaderDelegate?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.shield.layer.cornerRadius = 15
        self.shield.layer.borderWidth = 1
        self.shield.layer.borderColor = UIColor.white.cgColor
        self.shield.layer.masksToBounds = true
        self.trust.layer.cornerRadius = 15
        self.trust.layer.borderWidth = 1
        self.trust.layer.borderColor = UIColor.white.cgColor
        self.trust.layer.masksToBounds = true
        self.icon.layer.cornerRadius = 30
        self.icon.layer.masksToBounds = true
        self.backgroundColor = kDarkBackgroundColor
    }
    
    func configerModel(model: AdvertisingListModel) {
        if let str = model.portrait, let url = URL(string: str) {
            self.icon.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "login_icon"))
        }else {
            self.icon.image = #imageLiteral(resourceName: "login_icon")
        }
        
        if model.isTrust == 0{
            self.isTrust = false
        }else  {
            self.isTrust = true
        }
        
        if model.isBlack == 0 {
            self.isShield = false
        }else {
            self.isShield = true
        }
        
        self.changeTrust()
        self.changeShield()
        
        self.name.text = model.userName
        self.exchange.text = "\(model.dealCount ?? 0)"
        self.good.text = model.highOpinionRatio
        self.trustNum.text = "\(model.trustNum ?? 0)"
    }
    
    func changeTrust() {
        if isTrust {
            self.trust.setTitle(kText("已信任"), for: .normal)
            self.trust.setImage(nil, for: .normal)
        }else {
            self.trust.setTitle(kText("信任"), for: .normal)
            self.trust.setImage(#imageLiteral(resourceName: "tradeUserAdd"), for: .normal)
        }
    }
    
    func changeShield() {
        if isShield {
            self.shield.setTitle(kText("已屏蔽"), for: .normal)
            self.shield.setImage(nil, for: .normal)
        }else {
            self.shield.setTitle(kText("屏蔽"), for: .normal)
            self.shield.setImage(#imageLiteral(resourceName: "tradeUserAdd"), for: .normal)
        }
    }
    
    @IBAction func trustClick(_ sender: UIButton) {
        self.delegate?.trustClick(header: self)
    }
    
    @IBAction func shieldClick(_ sender: UIButton) {
        self.delegate?.shieldClick(header: self)
    }
    

}
